//
//  LogoutViewController.m
//  WDFans
//
//  Created by Apple Inc on 4/3/12.
//  Copyright (c) 2012 AppleInc. All rights reserved.
//

#import "LogoutViewController.h"
#import "ZHDataService.h"
#import "ActivityViewManager.h"
#import "LoginViewController.h"
#import "ForwardWeiboViewController.h"
#import "LocalUser.h"
#import "RootViewController.h"
#import "PersonalViewController.h"
//#import "OAConsumer.h"
//#import "OAToken.h"

@interface LogoutViewController ()

@end

@implementation LogoutViewController

@synthesize isWeibo,tv;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark - implemeted absract methods in abstract class
- (void)rightItemClicked
{
    if (isWeibo == NO) {
        [ZHDataService logout];
        [ActivityViewManager showErrorViewWithMessage:@"已注销"];
    }
    else {
        [[ZHDataService sharedService].engine signOut];
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
//    self.navigationItem.rightBarButtonItem = nil;
//    
//    [self.tv reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    if (isWeibo == NO) {
        if([ZHDataService isLogined])
        {
            [self addNaviBarRightItemWithTitle:@"注销"];
        }
        else {
            self.navigationItem.rightBarButtonItem = nil;
        }
    }
    else {
        if([[ZHDataService sharedService].engine isAuthorized])
        {
            [self addNaviBarRightItemWithTitle:@"注销"];
        }
        else {
            self.navigationItem.rightBarButtonItem = nil;
        }
    }
    [self.tv reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(self.isWeibo)
        self.title = @"微博账号";
    else
        self.title = @"应用账号";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.tv = nil;
}

#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{   
    if(self.isWeibo)
        return 1;
    else
    {
        if([[ZHDataService sharedService].localUsers count] == 0)
            return 1;
        else 
            return 2;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.isWeibo)
        return 1;
    else
    {
        if([[ZHDataService sharedService].localUsers count] == 0)
            return 1;
        
        if(section == 0)
            return [[ZHDataService sharedService].localUsers count];
        else
            return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cellIdentify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
        cell.textLabel.textColor = [UIColor cellLeftKeyTextColor];
        cell.textLabel.font = [UIFont getSettingViewTableCellLabelFont];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.detailTextLabel.text = nil;
    if (isWeibo == NO) {
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        if([[ZHDataService sharedService].localUsers count] == 0)
        {
            cell.textLabel.text = @"点击添加账号";
        }
        else
        {
            if(indexPath.section == 0)
            {
                LocalUser *lu = [[ZHDataService sharedService].localUsers objectAtIndex:indexPath.row];
                cell.textLabel.text = lu.name;
                if([lu.name isEqualToString:[ZHDataService sharedService].loginUser.userName])
                {
                    cell.detailTextLabel.text = @"已登陆";
                    cell.selectionStyle = UITableViewCellSelectionStyleNone;
                }
                else
                    cell.detailTextLabel.text = @"点击登录";
            }
            else
            {
                cell.textLabel.text = @"点击添加账号";
            }
        }
    }
    else {
        if(![[ZHDataService sharedService].engine isAuthorized])
        {
            cell.textLabel.text = @"未绑定 点击绑定新浪微博";
        }
        else {
            cell.textLabel.text = @"已绑定新浪微博";
        }
    }
    return cell;
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (isWeibo == NO) {
        if([[ZHDataService sharedService].localUsers count] == 0)
            [self.tabBarController setSelectedIndex:1];
        else
        {
            if(indexPath.section == 1)
            {
                [ZHDataService logout];
                RootViewController *rootController = (RootViewController *)(self.navigationController.tabBarController);
                PersonalViewController *personController = nil;
                
                UINavigationController *nc = (UINavigationController *)[[rootController viewControllers] objectAtIndex:1];
                
                [nc popToRootViewControllerAnimated:NO];
                personController = (PersonalViewController *)[nc topViewController];
                personController.username = nil;
                personController.password = nil;
                personController.isAutoLogin = NO;
                [self.tabBarController setSelectedIndex:1];
            }
            if(indexPath.section != 1)
            {
                
                LocalUser *lu = [[ZHDataService sharedService].localUsers objectAtIndex:indexPath.row];
                if([lu.name isEqualToString:[ZHDataService sharedService].loginUser.userName])
                    return;
                RootViewController *rootController = (RootViewController *)(self.navigationController.tabBarController);
                PersonalViewController *personController = nil;
                
                UINavigationController *nc = (UINavigationController *)[[rootController viewControllers] objectAtIndex:1];
                
                [nc popToRootViewControllerAnimated:NO];
                personController = (PersonalViewController *)[nc topViewController];
                personController.username = lu.name;
                personController.password = lu.pwd;
                personController.isAutoLogin = lu.isAuto;
                personController.currentTab = kPersonalComprehensiveTab;
                
                 [ZHDataService logout];
                [self.tabBarController setSelectedIndex:1];
                //view chuxian de xianhou shunxu, daozhi chenxian de bu zhengchang.
                //[personController performSelector:@selector(tabClicked:) withObject:personController.comprehensiveButton afterDelay:0.0];
            }
        }
    }
    else {
        if(![[ZHDataService sharedService].engine isAuthorized])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:kPostWeiBoNotification object:nil userInfo:nil];
        }
    }
}

#pragma mark - WBSessionDelegate methods
- (void)weiboDidLogin
{
    [ActivityViewManager showErrorViewWithMessage:@"新浪微博绑定成功！"];
}
- (void)weiboLoginFailed:(BOOL)userCancelled withError:(NSError*)error
{
    [ActivityViewManager showErrorViewWithMessage:@"新浪微博绑定失败！"];
}

- (void)weiboDidLogout
{
    [ActivityViewManager showErrorViewWithMessage:@"新浪微博取消绑定！"];
}

@end
